﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.Errors;
using EffiProz.Core.RowIO;
using EffiProz.Core.Results;

namespace EffiProz.Core.Navigators
{
    public abstract class RowSetNavigator : RangeIterator
    {
        public RowSetNavigator() { }

        protected SessionInterface session;
        protected long id;
        public int size;
        public int mode;
        public bool isIterator;
        public int currentPos = -1;

        /**
         * Sets the id;
         */
        public void setId(long id)
        {
            this.id = id;
        }

        /**
         * Returns the id
         */
        public long getId()
        {
            return id;
        }

        /**
         * Returns the current row object. Type of object is implementation defined.
         */
        public abstract Object[] getCurrent();

        public long getRowid()
        {
            return 0;
        }

        public Object getRowidObject()
        {
            return null;
        }

        public abstract Row getCurrentRow();

        /**
         * Add row to the end
         */
        public abstract void add(Object data);

        /**
         * Remove current row
         */
        public abstract void remove();

        /**
         * Clear all rows
         */
        public abstract void clear();

        /**
         * Reset to initial state
         */
        public virtual void reset()
        {
            currentPos = -1;
        }

        /**
         * Remove any resourses and invalidate
         */
        public void release()
        {
            reset();
        }

        public void setSession(SessionInterface session)
        {
            this.session = session;
        }

        public SessionInterface getSession()
        {
            return session;
        }

        public int getSize()
        {
            return size;
        }

        public bool isEmpty()
        {
            return size == 0;
        }

        public Object[] getNext()
        {
            return next() ? getCurrent()
                          : null;
        }

        public virtual bool next()
        {

            if (hasNext())
            {
                currentPos++;

                return true;
            }
            else if (size != 0)
            {
                currentPos = size;
            }

            return false;
        }

        public bool hasNext()
        {
            return currentPos < size - 1;
        }

        public bool beforeFirst()
        {

            reset();

            currentPos = -1;

            return true;
        }

        public bool afterLast()
        {

            if (size == 0)
            {
                return false;
            }

            reset();

            currentPos = size;

            return true;
        }

        public bool first()
        {

            beforeFirst();

            return next();
        }

        public bool last()
        {

            if (size == 0)
            {
                return false;
            }

            if (isAfterLast())
            {
                beforeFirst();
            }

            while (hasNext())
            {
                next();
            }

            return true;
        }

        public int getRowNumber()
        {
            return currentPos;
        }

        /**
         * Uses similar semantics to java.sql.ResultSet except this is 0 based.
         * When position is 0 or positive, it is from the start; when negative,
         * it is from end
         */
        public virtual bool absolute(int position)
        {

            if (position < 0)
            {
                position += size;
            }

            if (position < 0)
            {
                beforeFirst();

                return false;
            }

            if (position > size)
            {
                afterLast();

                return false;
            }

            if (size == 0)
            {
                return false;
            }

            if (position < currentPos)
            {
                beforeFirst();
            }

            // go to the tagget row;
            while (position > currentPos)
            {
                next();
            }

            return true;
        }

        public bool relative(int rows)
        {

            int position = currentPos + rows;

            if (position < 0)
            {
                beforeFirst();

                return false;
            }

            return absolute(position);
        }

        public bool previous()
        {
            return relative(-1);
        }

        public bool isFirst()
        {
            return size > 0 && currentPos == 0;
        }

        public bool isLast()
        {
            return size > 0 && currentPos == size - 1;
        }

        public bool isBeforeFirst()
        {
            return size > 0 && currentPos == -1;
        }

        public bool isAfterLast()
        {
            return size > 0 && currentPos == size;
        }

        public virtual void close() { }

        public virtual void writeSimple(RowOutputInterface _out,
                                ResultMetaData meta)
        {
            throw Error.runtimeError(ErrorCode.U_S0500, "RowSetNavigator");
        }

        public virtual void readSimple(RowInputInterface _in,
                               ResultMetaData meta)
        {
            throw Error.runtimeError(ErrorCode.U_S0500, "RowSetNavigator");
        }

        public abstract void write(RowOutputInterface _out,
                                   ResultMetaData meta);

        public abstract void read(RowInputInterface _in,
                                  ResultMetaData meta);

        public virtual bool isMemory()
        {
            return true;
        }

        public int getRangePosition()
        {
            return 0;
        }
    }
}
